package com.youthfilmic.yuejian.service;

import com.youthfilmic.yuejian.dao.CertificationDao;
import com.youthfilmic.yuejian.dao.UserDao;
import com.youthfilmic.yuejian.domain.Certification;
import com.youthfilmic.yuejian.domain.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created by youweixi on 15/10/12.
 */
@Service
public class CertificationServiceImpl implements CertificationService{
    @Resource
    private CertificationDao certificationDao;
    @Resource
    private UserDao userDao;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void handleCertification(Long certificationId , int status ,String reason) throws Exception {
        Certification c = certificationDao.findOne(certificationId);
        User user = null;
        if (c == null){
            throw new Exception();
        }else{
            c.setHandleAt(new Date());
            if (status == 1){
                c.setStatus(Certification.STATUS_SUCCEED);
                user = c.getUser();
                if (user == null || user.isCertificationAccepted() == true) throw new Exception();
                user.setBirthday(c.getBirthday());
                user.setCity(c.getCity());
                user.setEmail(c.getEmail());
                user.setInstruction(c.getInstruction());
                user.setJobYears(c.getJobYears());
                user.setPhone(c.getPhone());
                user.setPresenter(c.getPresenter());
                user.setProfession(c.getProfession());
                user.setRealName(c.getRealName());
                user.setReference(c.getReference());
                user.setCertificationAccepted(true);
                user.setCompany(c.getCompany());
                if (c.getImg() != null) user.setImg(c.getImg());
                if (c.getPicUrl() != null) user.setPicUrl(c.getPicUrl());
                userDao.save(user);
            } else if(status == -1){
                c.setStatus(Certification.STATUS_FAILED);
                c.setReason(reason);
            } else if (status == 0){
                //更改状态到未审核，所有状态都回写到certification里面
                c.setStatus(Certification.STATUS_WAIT_FOR_CERTIFICATE);
                user = c.getUser();
                //注意这里，用户也要改成未认证的
                user.setCertificationAccepted(false);
                userDao.save(user);
                c.setBirthday(user.getBirthday());
                c.setCity(user.getCity());
                c.setEmail(user.getEmail());
                c.setInstruction(user.getInstruction());
                c.setJobYears(user.getJobYears());
                c.setPhone(user.getPhone());
                c.setPresenter(user.getPresenter());
                c.setProfession(user.getProfession());
                c.setRealName(user.getRealName());
                c.setReference(user.getReference());
                c.setCompany(user.getCompany());
                if (user.getImg() != null) c.setImg(user.getImg());
                if (user.getPicUrl() != null) c.setPicUrl(user.getPicUrl());
            }else{
                throw new Exception();
            }
            certificationDao.save(c);
        }

        //短信通知
        if (status == 1){
            MobileServiceImpl.SendMobileMessageForContent(c.getPhone() , MobileServiceImpl.CERTIFICATION_PASS);
        }else if (status == 2){
            MobileServiceImpl.SendMobileMessageForContent(c.getPhone() , MobileServiceImpl.CERTIFICATION_DENY);
        }
        
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCertificationInfo(Long certificationId, Certification certification) {
        Certification c = certificationDao.findOne(certificationId);
        c.setBirthday(certification.getBirthday());
        c.setCity(certification.getCity());
        c.setCompany(certification.getCompany());
        c.setEmail(certification.getEmail());
        c.setInstruction(certification.getInstruction());
        c.setJobYears(certification.getJobYears());
        c.setPhone(certification.getPhone());
        c.setPresenter(certification.getPresenter());
        c.setRealName(certification.getRealName());
        c.setReference(certification.getReference());
        c.setProfession(certification.getProfession());
        if (certification.getImg() != null) c.setImg(certification.getImg());
        if (certification.getPicUrl() != null) c.setPicUrl(certification.getPicUrl());
        certificationDao.save(c);
    }
}
